package com.infore.water.mapper;

import com.infore.water.entity.AlarmRecord;
import com.infore.water.entity.AlarmStandard;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author:wk
 * @date:Created in 13:33 2017\11\14 0014
 */
@Mapper
public interface DataMapper {

    @Select("select a.id,a.site_name siteName,a.station_name stationName,a.alarm_type alarmType,a.time,a.acontent content,a.person,a.alarm_step alarmStep from T_Alarm_Record a left join T_EnvStation b on a.station_Id=b.id where a.station_id=#{stationId} or b.site_id=#{siteId}")
    List<AlarmRecord> getAlarmRecord(@Param("stationId") Integer stationId,@Param("siteId") Integer siteId);

    @Select("select a.id,a.site_name siteName,a.station_name stationName,a.alarm_type alarmType,a.time,a.acontent content,a.person,a.alarm_step alarmStep from T_Alarm_Record a left join T_EnvStation b on a.station_Id=b.id ")
    List<AlarmRecord> getAlarmRecordWithoutStationId();

    @Select("select a.id,a.site_name siteName,a.station_name stationName,a.alarm_type alarmType,a.time,a.acontent content,a.person,a.alarm_step alarmStep from T_Alarm_Record a left join T_EnvStation b on a.station_Id=b.id where (a.station_id=#{stationId} or b.site_id=#{siteId}) and a.alarm_type=#{alarmType}")
    List<AlarmRecord> getAlarmRecordByType(@Param("stationId") Integer stationId,@Param("siteId") Integer siteId,@Param("alarmType")String alarmType);

    @Select("select a.id,a.site_name siteName,a.station_name stationName,a.alarm_type alarmType,a.time,a.acontent content,a.person,a.alarm_step alarmStep from T_Alarm_Record a left join T_EnvStation b on a.station_Id=b.id where a.alarm_type=#{alarmType}")
    List<AlarmRecord> getAlarmRecordByTypeWithoutStationId(@Param("alarmType")String alarmType);


    @Select("select a.id,a.ALARM_TYPE alarmType,a.ALARM_STEP alarmStep,a.ALARM_PERSON alarmPerson,a.workDay,a.time,a.interval,a.stationIds stationId,b.name stationName,a.alarmLevel,a.ProjIDs projects from T_Base_Alarm_Rule a left join T_EnvStation b on a.stationIds=b.id where a.stationIds=#{stationId} or b.site_id=#{siteId}")
    List<AlarmStandard> getStandardList(@Param("stationId") Integer stationId, @Param("siteId") Integer siteId);

    @Select("select a.id,a.ALARM_TYPE alarmType,a.ALARM_STEP alarmStep,a.ALARM_PERSON alarmPerson,a.workDay,a.time,a.interval,a.stationIds stationId,b.name stationName,a.alarmLevel,a.ProjIDs projects from T_Base_Alarm_Rule a left join T_EnvStation b on a.stationIds=b.id ")
    List<AlarmStandard> getStandardListWithoutStationId();


    @Select("select a.id,a.ALARM_TYPE alarmType,a.ALARM_STEP alarmStep,a.ALARM_PERSON alarmPerson,a.workDay,a.time,a.interval,a.stationIds stationId,b.name stationName,a.alarmLevel,a.ProjIDs projects from T_Base_Alarm_Rule a left join T_EnvStation b on a.stationIds=b.id where (a.stationIds=#{stationId} or b.site_id=#{siteId}) and a.alarm_type=#{alarmType}")
    List<AlarmStandard> getStandardListByType(@Param("stationId") Integer stationId, @Param("siteId") Integer siteId,@Param("alarmType")String alarmType);

    @Select("select a.id,a.ALARM_TYPE alarmType,a.ALARM_STEP alarmStep,a.ALARM_PERSON alarmPerson,a.workDay,a.time,a.interval,a.stationIds stationId,b.name stationName,a.alarmLevel,a.ProjIDs projects from T_Base_Alarm_Rule a left join T_EnvStation b on a.stationIds=b.id where a.alarm_type=#{alarmType}")
    List<AlarmStandard> getStandardListByTypeWithoutStationId(@Param("alarmType")String alarmType);


    @Select("select a.id stationId,a.name stationName,c.id projectId,c.name projectName from T_EnvStation a left join T_Mid_ProjectStation b on a.id=b.station_id left join T_EnvProject c on b.project_id=c.id left join T_mid_SiteRole r on a.site_id=r.site_id where r.role_id=#{roleId}")
    List<Map<String,Object>> getStationProject(@Param("roleId")String roleId);

    @Select("select id,name from T_Base_AlarmContact ")
    List<Map<String,Object>> getAlarmPerson();

    @Insert("insert into T_Base_ALArm_rule (ALARM_TYPE,ALARM_STEP,WORKDAY,[TIME],INTERVAL,ALARMLEVEL,StationIDs) values (#{alarmType},#{alarmStep},#{workDay},#{times},#{interval},#{alarmLevel},#{stationId})")
    int setAlarmStandard(@Param("alarmType")String alarmType,@Param("alarmStep")String alarmStep,@Param("workDay")String workDay,@Param("times")String time,@Param("interval")Integer interval,@Param("alarmLevel")String alarmLevel,@Param(value = "stationId")Integer stationId);

    @Update("update T_Base_ALArm_rule set ALARM_TYPE=#{alarmType},ALARM_STEP=#{alarmStep},WORKDAY=#{workDay},TIME=#{time},INTERVAL=#{interval},ALARMLEVEL=#{alarmLevel},StationIDs=#{stationId} where id=#{id}")
    int updateAlarmStandard(@Param("alarmType")String alarmType,@Param("alarmStep")String alarmStep,@Param("workDay")String workDay,@Param("time")String time,@Param("interval")String interval,@Param("alarmLevel")String alarmLevel,@Param(value = "stationId")Integer stationId,@Param("id")Integer id);

    @Select("select a.name,a.id from T_Base_AlarmContact a left join T_Base_Mid_Rule_Contact b on a.id=b.alarm_contact_id where b.alarm_rule_id=#{id}")
    List<Map<String,Object>> getAlarmPersonByAlarmId(@Param("id")Integer id);

    @Select("select * from T_Base_ALARM_RULE a where a.stationIds=#{stationId} and alarm_type=#{type}")
    List<Map<String,Object>> getAlarmStandardByStationId(@Param("stationId")Integer stationId,@Param("type")String type);

    @Insert("insert into T_Base_Mid_rule_contact (alarm_rule_id,alarm_contact_id) values (#{ruleId},#{contactId})")
    int updateAlarmPerson(@Param("ruleId")Integer ruleId,@Param("contactId")Integer contactId);

    @Delete("delete T_Base_Mid_rule_contact where alarm_rule_id=#{ruleId}")
    void deleteAlarmPerson(@Param("ruleId")Integer ruleId);

    @Delete("delete T_Alarm_Record where id=#{id}")
    void deleteAlarmRecord(@Param("id")Integer id);

    @Delete("delete T_Base_Alarm_rule where id=#{id}")
    void deleteAlarmStandard(@Param("id")Integer id);
}
